//
//  CTNavBar.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit

public enum CTNavLeftStyle {
    case back
    case title(NSAttributedString)
    case image(UIImage?)
}

public enum CTNavTitleStyle {
    case `default`
    case custom(titleColor: UIColor, bgColor: UIColor)
    case attrTitle(NSAttributedString)
}

public protocol CTNavViewDelegate: NSObjectProtocol {
    func navleftViewTapped()
}

public class CTNavView: UIView {
    
    public weak var delegate: CTNavViewDelegate?
    public let alphaBgView = UIView()
    public let leftView = UIButton()
    public let titleLabel = UILabel()
    public let separatorLine = UIView()
    
    private var navTitle: String
    public init(title: String = "", delegate: CTNavViewDelegate? = nil) {
        self.navTitle = title
        self.delegate = delegate
        super.init(frame: .zero)
        
        setupLayout()
        setupStyle()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - ********* public method for style setting
public extension CTNavView {
    
    var title: String {
        get { return titleLabel.text ?? "" }
        set { titleLabel.text = newValue }
    }
    
    func setupLeftStyle(_ style: CTNavLeftStyle) {
        switch style {
        case .back:
            leftView.setImage(.icon(.nav_back), for: .normal)
        case .title(let attrStr):
            leftView.setImage(nil, for: .normal)
            leftView.setAttributedTitle(attrStr, for: .normal)
        case .image(let image):
            leftView.setImage(image, for: .normal)
        }
    }
    
    func setTitleStyle(_ style: CTNavTitleStyle) {
        switch style {
        case .default:
            titleLabel.textColor = .color(.theme)
            self.backgroundColor = .color(.white)
        case .custom(let titleColor,let bgColor):
            titleLabel.textColor = titleColor
            self.backgroundColor = bgColor
        case .attrTitle(let attrStr):
            titleLabel.attributedText = attrStr
        }
    }
}

// MARK: - ********* response
extension CTNavView {
    @objc func leftViewTapped() {
        delegate?.navleftViewTapped()
    }
}

private extension CTNavView {
    
    func setupLayout() {
        addSubview(alphaBgView)
        addSubview(titleLabel)
        addSubview(leftView)
        addSubview(separatorLine)
        
        self.frame = CGRect(x: 0, y: 0, width: kScreenWid, height: kNavBtm)
        alphaBgView.frame = CGRect(x: 0, y: 0, width: kScreenWid, height: kNavBtm)
        separatorLine.frame = CGRect(x: 0, y: kNavBtm - 1, width: kScreenWid, height: 1)
        
        titleLabel.snp.makeConstraints { (m) in
            m.leading.trailing.equalToSuperview().inset(60)
            m.top.equalTo(kNavTop)
            m.height.equalTo(44)
        }
        leftView.snp.makeConstraints { (m) in
            m.leading.equalToSuperview()
            m.top.equalTo(kNavTop)
            m.bottom.equalToSuperview()
            m.width.greaterThanOrEqualTo(48)
        }
    }
    
    func setupStyle() {
        backgroundColor = UIColor.white
        alphaBgView.backgroundColor = .color(.white)
        alphaBgView.alpha = 0
        separatorLine.backgroundColor = .color(.separateLine)
        separatorLine.isHidden = true
    
        titleLabel.do { (v) in
            v.textColor = .color(.navTitle)
            v.font = .semiBold(17, isFit: false)
            v.textAlignment = .center
            v.lineBreakMode = .byTruncatingTail
            v.text = navTitle
        }
        leftView.do { (v) in
            v.contentEdgeInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 0)
            v.contentHorizontalAlignment = .left
            v.setImage(.icon(.nav_back), for: .normal)
            v.addTarget(self, action: #selector(leftViewTapped), for: .touchUpInside)
        }
    }
}

